<?php
	require_once($_SERVER["DOCUMENT_ROOT"]."/global.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/modules/security_mod.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/alerter/modules/account_mod.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/modules/level_mod.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/page_navigator.php");

	SecurityMod::login();

	$level = $_SESSION["account"]->getLevel();
	if ($level != "Admin")
		SecurityMod::logout();

	$pageNumber = intval(v($_REQUEST, "page"));
	if ($pageNumber < 1)
		$pageNumber = 1;

	function drawNavigator() {
		global $pageCount, $pageNumber;
		$params = "";
		PageNavigator::draw($pageCount, $pageNumber, 33, $params, "alert_accounts.php");
	}

	$accounts = AlertAccountMod::getAccounts($pageNumber, $pageCount);

	$levels = LevelMod::getLevels();
?>
<html>
<head>
<title><?php echo(SettingsMod::PAGE_TITLE." :: Accounts"); ?></title>
<link rel="stylesheet" href="main.css">
</head>
<body>
	<h2 align="center">Alerter Account List</h2>
	<table align="center" width="600">
	<tr>
		<td align="center">
			<table background="<?php echo(SettingsMod::STATIC_IMAGES)?>/bgd.gif" class="messagestyle" align="center" width="100%">
			<tr>
				<td colspan="10"><?php drawNavigator()?></td>
			</tr>
			<tr>
				<th>&nbsp;</th>
				<th><u>Name</u></th>
				<th><u>Level</u></th>
				<th><u>Validation</u></th>
				<th><u>Delete</u></th>
			</tr>
			<?php
				$i = 0;
				foreach ($accounts as $acc):
				$i++;
			?>
			<tr bgcolor='#0b0b2f'>
				<td align='right' style='cursor:crosshair'>
					<?php echo(($pageNumber - 1) * SettingsMod::PAGE_RECORDS_PER_PAGE + $i)?>.
				</td>
				<td><?php $acc->drawName(); ?></td>
				<td><?php echo($acc->getLevel()); ?></td>
				<!-- Validation-->
				<form method="post" action="alert_account_validation.php">
				<td>
					<table width="100%">
						<tr>
							<td style="text-align: center">
								<?php print($acc->getValidation()); ?>
							</td>
							<td style="text-align: center">
								<input type="hidden" name="name" value="<?php echo($acc->getName()); ?>" />
								<input type="hidden" name="level" value="<?php echo($acc->getLevel()); ?>" />
								<input type="submit" value="Change" />
							</td>
						</tr>
					</table>
				</td>
				</form>

				<!-- Delete -->
				<form action="alert_account_delete.php" method="get">
				<td style="text-align: center">
						<input type="hidden" name="name" value="<?php echo($acc->getName()); ?>" />
						<input type="submit" value="Delete" />
				</td>
				</form>
			</tr>
			<?php endforeach; ?>
			<tr>
				<td colspan="99"><?php drawNavigator()?></td>
			</tr>
		</td>
	</tr>
	</table>

	<br />
	<h2 align="center">Create Alerter Account</h2>
	<form method="post" action="alert_account_add.php">
	<table background="<?php echo(SettingsMod::STATIC_IMAGES)?>/bgd.gif" class="messagestyle" align="center">
	<tr>
		<th>Pilot Name:</th>
		<td><input type="text" name="pilot" style="width: 100%" /></td>
	</tr>
	<tr>
		<th>Security Level:</th>
		<td><input type="text" name="level" style="width: 100%" value="User" /></td>
	</tr>
	<tr>
		<th>Validation code:</th>
		<td><input type="text" name="validation" style="width: 100%" readonly="readonly" value="<?php print(uniqid('', true)); ?>" /></td>
	</tr>
	<tr>
		<td colspan="2" align="center"><input type="submit" value="Create Account" style="width: 100%" /></td>
	</tr>
	</table>
	</form>
</body>
</html>